package com.jiang.专题.位运算.Q67;

/**
 * @author Jiang
 * @version 1.0
 * @date 2025/08/31 15:07
 */
class Solution {
    public String addBinary(String a, String b) {
        StringBuilder res = new StringBuilder();
        int m = a.length();
        int n = b.length();
        int carry = 0;
        for (int i = m - 1, j = n - 1; i >= 0 || j >= 0; i--, j--) {
            int sum = carry;
            sum += i >= 0 ? a.charAt(i) - '0' : 0;
            sum += j >= 0 ? b.charAt(j) - '0' : 0;
            res.append(sum % 2);
            carry = sum / 2;
        }
        if (carry == 1) res.append("1");
        return res.reverse().toString();
    }
}
